﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

using LoggingServer;

namespace LoggingServerInstall
{
    class LoggingServerInstall
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [STAThread()]
        static void Main(string[] args)
        {
            Console.Title = "LoggingServer Installing...";
            RegistryHelper.RegistryUtils reg = new RegistryHelper.RegistryUtils();

            LoggingServer.StaticComponent.LogExBool = reg.GetRegVal(
                @"SOFTWARE\GAME_SERVER\Logging", "LogServerException");

            bool ConsoleVisiable = reg.GetRegVal(
                @"SOFTWARE\GAME_SERVER\Console", "ConsoleVisiable");

            Install install = new Install();

            Console.WriteLine(string.Format("[ExceptionOutput]:{0}", StaticComponent.LogExBool));
            Console.WriteLine(string.Format("[ConsoleVisiable]:{0}", ConsoleVisiable));

            if (install.Start())
            {
                Console.Title = "LoggingServer Is Starting...";
                Console.Title = "LoggingServer Is Running";

                setConsoleWindowVisibility(ConsoleVisiable, Console.Title);

                new LOGServer().Start();
            }
            else
            {
                Console.Title = "LoggingServer Install Failed...";
            } 
        }

        public static void setConsoleWindowVisibility(bool visible, string title)
        {       
            IntPtr hWnd = FindWindow(null, title);

            if (hWnd != IntPtr.Zero)
            {
                if (!visible)
                {
                    ShowWindow(hWnd, 0); // 0 = SW_HIDE 
                }
                else
                {
                    //Show window again                   
                    ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA 
                }
            }
        }
    }
}
